<?php
$baseURL = './CentralController.php?surveycode=Surveys'; 
$survey = $records['survey'];
$questions = $records['questions'];
$date = date("Y-m-d");  
if($survey['survey_id']==NULL){
	$action = "Add";
	$title = "Add new survey";
}
else{ 
	$action = "EditSurvey";
	$title = "Update survey";
	if($survey['survey_type']==0){
		$surveytype = "Survey";
	}else if($survey['survey_type']==1){
		$surveytype = "Poll";
	}else{
		$surveytype = "Quiz";
	}
	
}

?>
<script type="text/javascript" src="../../scripts/archive.js"></script>
<?php include ROOT_PATH."/lib/common/calendar.php"; ?>
<script type="text/javascript">

function returnAdd() {
		location.href = '<?php echo $baseURL;?>&action=ViewAddQuestion&surveyid=<?php echo $survey['survey_id'];?>';
}
function returnDelete() {
		var check = false;
		with (document.frmQuestion) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true)){
					check = true;
					break;
				}
			}

			if (check) {
				document.frmQuestion.action= "<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=DeleteQuestions";
				document.frmQuestion.submit();
			} else {
				alert("<?php echo $lang_Error_SelectAtLeastOneRecordToDelete; ?>");
			}
		}
	}
	
function addSave() {
	if(validate()=true)
		document.frmSurveys.submit();
	else
		return false;
}


function validate() {
	/*
	startDate = strToDate($("txtFromDate").value, YAHOO.OrangeHRM.calendar.format);
	endDate = strToDate($("txtToDate").value, YAHOO.OrangeHRM.calendar.format);

	errFlag=false;
	errors = new Array();
	if (!startDate || !endDate || (startDate > endDate)) {
		errors[errors.length] = "<?php echo $lang_Time_Errors_InvalidDateOrZeroOrNegativeRangeSpecified; ?>";
		errFlag=true;
	}

	if (errFlag) {
		errStr="<?php echo $lang_Common_EncounteredTheFollowingProblems; ?>\n";
		for (i in errors) {
			errStr+=" - "+errors[i]+"\n";
		}
		alert(errStr);

		return false;
	}
	*/
	return true;
}
/*
YAHOO.OrangeHRM.container.init();
YAHOO.util.Event.addListener($("frmReport"), "submit", viewProjectReport);
*/ 

</script>

<h2>
<?php echo $title; ?>
<hr/>
</h2>
<div id="status"></div>
<?php if (isset($_GET['message'])) {

		$expString  = $_GET['message'];
		//$col_def = CommonFunctions::getCssClassForMessage($expString);
		$expString = '$lang_Common_' . $expString;
?>
		<font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<?php echo $expString; ?>
		</font>
<?php }	?>
<form name="frmSurveys" id="frmSurveys" method="post"  action="<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=<?php echo $action; ?>">
<input type="hidden" name="txtId" value="<?php echo $survey['survey_id']; ?>" >
<table border="0" cellpadding="5" cellspacing="0">
	<thead>
		<tr>
			<th class="tableTopLeft"></th>
	    	<th class="tableTopMiddle" ></th>
	    	<th class="tableTopMiddle" ></th>
	    	<th class="tableTopMiddle"></th>
			<th class="tableTopRight"></th>
		</tr>
	</thead>
	<tbody>
		
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Surveys_Name; ?></td>
			<td ></td>
			<td >
				<input type="text" id="txtName" name="txtName" value="<?php echo $survey['survey_name'] ?>" size="43"/>
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Question_Surveys; ?></td>		
			<td></td>	
			<td >
				<?php if($survey['survey_id']==NULL){ ?>
				<select name="surveytype"  id="surveytype" size="1"  >
				<option value="0">Survey</option>
				<option value="1">Poll</option>
				<option value="2">Quiz</option>				
				</select>
				<?php }else{ ?>
				<input type="text" name="surveytypetext"  id="surveytypetext" value="<?php echo $surveytype;  ?>" READONLY >
				<input type="hidden" name="surveytype"  id="surveytype" value="<?php echo $survey['survey_type'];  ?>"  >
				<?php } ?>				
			</td>
			
			
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo "Survey header"; ?></td>
			<td ></td>
			<td >
				<TEXTAREA  id="txtDescription" name="txtDescription" COLS="40" ROWS="1"/><?php echo $survey['survey_description'] ?></TEXTAREA>
				
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Time_Common_FromDate; ?></td>
			<td ></td>
			<td >
				<input type="text" id="txtFromDate" name="txtFromDate" value="<?php echo date('Y-m-d',strtotime($survey['survey_from_date']?$survey['survey_from_date']:$date)) ?>" size="10"/>
				<input type="button" id="btnFromDate" name="btnFromDate" value="  " class="calendarBtn"/>
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Time_Common_ToDate; ?></td>
			<td ></td>
			<td >
				
				<input type="text" id="txtToDate" name="txtToDate" value="<?php echo date('Y-m-d',strtotime($survey['survey_to_date']?$survey['survey_to_date']:$date)) ?>" size="10"/>
				<input type="button" id="btnToDate" name="btnToDate" value="  " class="calendarBtn"/>
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo "Survey footer"; ?></td>
			<td ></td>
			<td >
				<TEXTAREA  id="txtThanks" name="txtThanks" value="" COLS="40" ROWS="1"/><?php echo $survey['survey_thanks'] ?></TEXTAREA>
				
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo "Allow user view results"; ?></td>
			<td ></td>
			<td >
				<?php if($survey['results_view']==1 )
						echo '<input type="checkbox" checked id="cbActive" name="cbShowResults" value="1"/>'; 
					else
						echo '<input type="checkbox" id="cbActive" name="cbShowResults" value="1"/>';
				?>
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ><?php echo $lang_Surveys_Active; ?></td>
			<td ></td>
			<td >
				<?php if($survey['survey_active']==1 )
						echo '<input type="checkbox" checked id="cbActive" name="cbActive" value="1"/>'; 
					else
						echo '<input type="checkbox" id="cbActive" name="cbActive" value="1"/>';
				?>
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
		
		<tr>
			<td class="tableMiddleLeft"></td>
			<td ></td>
			<td ></td>
			<td >
				<input type="image"  onclick="addSave();" name="btnSave" alt="Save"
					   src="../../themes/beyondT/pictures/btn_save.gif" />
			</td>
			<td class="tableMiddleRight"></td>
		</tr>
	</tbody>
	<tfoot>
	  	<tr>
			<td class="tableBottomLeft"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomMiddle"></td>
			<td class="tableBottomRight"></td>
		</tr>
  	</tfoot>
</table>
</form>
<div id="cal1Container" style="position:absolute;" ></div>
<?php if(isset($survey['survey_id'])){?>
<h2><?php echo $lang_Questions_List; ?><hr/></h2>
<form id="frmQuestion" name="frmQuestion" method="post" action="">
<input type="hidden" name="surveyId" value="<?php echo $survey['survey_id']; ?>" >
<div style="padding: 6px;">
				<img 
					title="Add" 
					alt="Add"
					src="../../themes/beyondT/pictures/btn_add.gif" 
					style="border: none;"
					onclick="returnAdd();"
					onmouseout="this.src='../../themes/beyondT/pictures/btn_add.gif';" 
					onmouseover="this.src='../../themes/beyondT/pictures/btn_add_02.gif';" />	
					 
					<img 
						title="Delete" 
						alt="Delete" 
						src="../../themes/beyondT/pictures/btn_delete.gif" 
						style="border: none;"
						onclick="returnDelete();"
						onmouseout="this.src='../../themes/beyondT/pictures/btn_delete.gif';"
						onmouseover="this.src='../../themes/beyondT/pictures/btn_delete_02.gif';" />
					
</div>
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
		<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" width="500" ></th>
    	<th class="tableTopMiddle"></th>
		<th class="tableTopRight"></th>
	</tr>
	<tr>
		<th class="tableMiddleLeft"></th>
		<th class="tableMiddleMiddle"></th>
    	<th class="tableMiddleMiddle"><?php echo $lang_Question_Text; ?></th>
    	<th  class="tableMiddleMiddle"></th>    	
		<th class="tableMiddleRight"></th>
	</tr>
  </thead>
  <tbody>
<?php
	$j = 0;
	if (is_array($questions)) {		
		foreach ($questions as $record) {
			//$survey = Surveys::getSurvey($record->getSurveyId()); 
			//$surveyOwner = 
			if(!($j%2)) {
				$cssClass = 'odd';
			 } else {
			 	$cssClass = 'even';
			 }
			 $j++;

?>
  <tr>
  	<td class="tableMiddleLeft"></td>
  	<td class="<?php echo $cssClass?>" align="center"> <input type='checkbox' class='checkbox' name='chkID[]' value='<?php echo $record->getId();?>'></td>
    <td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=ShowEditQuestion&id=<?php echo $record->getId(); ?>" target="rightMenu"><?php echo $record->getText(); ?></a></td>
     <td class="<?php echo $cssClass; ?>"><?php //echo $survey->getSurveyName(); ?></td>  
	<td class="tableMiddleRight"></td>
  </tr>

<?php
		}
	}
?>
  </tbody>
  <tfoot>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>					
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>
</form>

<?php } ?>
